using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace LMSData
{
	/// <summary>
	/// Strongly-typed collection for the Group class.
	/// </summary>
	[Serializable]
	public partial class GroupCollection : ActiveList<Group, GroupCollection> 
	{	   
		public GroupCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Group table.
	/// </summary>
	[Serializable]
	public partial class Group : ActiveRecord<Group>
	{
		#region .ctors and Default Settings
		
		public Group()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Group(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Group(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Group(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Group", TableType.Table, DataService.GetInstance("LMS"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarGroupID = new TableSchema.TableColumn(schema);
				colvarGroupID.ColumnName = "GroupID";
				colvarGroupID.DataType = DbType.Int32;
				colvarGroupID.MaxLength = 0;
				colvarGroupID.AutoIncrement = true;
				colvarGroupID.IsNullable = false;
				colvarGroupID.IsPrimaryKey = true;
				colvarGroupID.IsForeignKey = false;
				colvarGroupID.IsReadOnly = false;
				colvarGroupID.DefaultSetting = @"";
				colvarGroupID.ForeignKeyTableName = "";
				schema.Columns.Add(colvarGroupID);
				
				TableSchema.TableColumn colvarGroupName = new TableSchema.TableColumn(schema);
				colvarGroupName.ColumnName = "GroupName";
				colvarGroupName.DataType = DbType.String;
				colvarGroupName.MaxLength = 255;
				colvarGroupName.AutoIncrement = false;
				colvarGroupName.IsNullable = false;
				colvarGroupName.IsPrimaryKey = false;
				colvarGroupName.IsForeignKey = false;
				colvarGroupName.IsReadOnly = false;
				colvarGroupName.DefaultSetting = @"";
				colvarGroupName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarGroupName);
				
				TableSchema.TableColumn colvarDescription = new TableSchema.TableColumn(schema);
				colvarDescription.ColumnName = "Description";
				colvarDescription.DataType = DbType.String;
				colvarDescription.MaxLength = 4000;
				colvarDescription.AutoIncrement = false;
				colvarDescription.IsNullable = true;
				colvarDescription.IsPrimaryKey = false;
				colvarDescription.IsForeignKey = false;
				colvarDescription.IsReadOnly = false;
				colvarDescription.DefaultSetting = @"";
				colvarDescription.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDescription);
				
				TableSchema.TableColumn colvarCreatedOn = new TableSchema.TableColumn(schema);
				colvarCreatedOn.ColumnName = "CreatedOn";
				colvarCreatedOn.DataType = DbType.DateTime;
				colvarCreatedOn.MaxLength = 0;
				colvarCreatedOn.AutoIncrement = false;
				colvarCreatedOn.IsNullable = true;
				colvarCreatedOn.IsPrimaryKey = false;
				colvarCreatedOn.IsForeignKey = false;
				colvarCreatedOn.IsReadOnly = false;
				colvarCreatedOn.DefaultSetting = @"";
				colvarCreatedOn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreatedOn);
				
				TableSchema.TableColumn colvarCreatedBy = new TableSchema.TableColumn(schema);
				colvarCreatedBy.ColumnName = "CreatedBy";
				colvarCreatedBy.DataType = DbType.String;
				colvarCreatedBy.MaxLength = 50;
				colvarCreatedBy.AutoIncrement = false;
				colvarCreatedBy.IsNullable = true;
				colvarCreatedBy.IsPrimaryKey = false;
				colvarCreatedBy.IsForeignKey = false;
				colvarCreatedBy.IsReadOnly = false;
				colvarCreatedBy.DefaultSetting = @"";
				colvarCreatedBy.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreatedBy);
				
				TableSchema.TableColumn colvarModifiedOn = new TableSchema.TableColumn(schema);
				colvarModifiedOn.ColumnName = "ModifiedOn";
				colvarModifiedOn.DataType = DbType.DateTime;
				colvarModifiedOn.MaxLength = 0;
				colvarModifiedOn.AutoIncrement = false;
				colvarModifiedOn.IsNullable = true;
				colvarModifiedOn.IsPrimaryKey = false;
				colvarModifiedOn.IsForeignKey = false;
				colvarModifiedOn.IsReadOnly = false;
				colvarModifiedOn.DefaultSetting = @"";
				colvarModifiedOn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarModifiedOn);
				
				TableSchema.TableColumn colvarModifiedBy = new TableSchema.TableColumn(schema);
				colvarModifiedBy.ColumnName = "ModifiedBy";
				colvarModifiedBy.DataType = DbType.String;
				colvarModifiedBy.MaxLength = 50;
				colvarModifiedBy.AutoIncrement = false;
				colvarModifiedBy.IsNullable = true;
				colvarModifiedBy.IsPrimaryKey = false;
				colvarModifiedBy.IsForeignKey = false;
				colvarModifiedBy.IsReadOnly = false;
				colvarModifiedBy.DefaultSetting = @"";
				colvarModifiedBy.ForeignKeyTableName = "";
				schema.Columns.Add(colvarModifiedBy);
				
				TableSchema.TableColumn colvarIsDeleted = new TableSchema.TableColumn(schema);
				colvarIsDeleted.ColumnName = "IsDeleted";
				colvarIsDeleted.DataType = DbType.Boolean;
				colvarIsDeleted.MaxLength = 0;
				colvarIsDeleted.AutoIncrement = false;
				colvarIsDeleted.IsNullable = false;
				colvarIsDeleted.IsPrimaryKey = false;
				colvarIsDeleted.IsForeignKey = false;
				colvarIsDeleted.IsReadOnly = false;
				
						colvarIsDeleted.DefaultSetting = @"((0))";
				colvarIsDeleted.ForeignKeyTableName = "";
				schema.Columns.Add(colvarIsDeleted);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["LMS"].AddSchema("Group",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("GroupID")]
		public int GroupID 
		{
			get { return GetColumnValue<int>("GroupID"); }

			set { SetColumnValue("GroupID", value); }

		}

		  
		[XmlAttribute("GroupName")]
		public string GroupName 
		{
			get { return GetColumnValue<string>("GroupName"); }

			set { SetColumnValue("GroupName", value); }

		}

		  
		[XmlAttribute("Description")]
		public string Description 
		{
			get { return GetColumnValue<string>("Description"); }

			set { SetColumnValue("Description", value); }

		}

		  
		[XmlAttribute("CreatedOn")]
		public DateTime? CreatedOn 
		{
			get { return GetColumnValue<DateTime?>("CreatedOn"); }

			set { SetColumnValue("CreatedOn", value); }

		}

		  
		[XmlAttribute("CreatedBy")]
		public string CreatedBy 
		{
			get { return GetColumnValue<string>("CreatedBy"); }

			set { SetColumnValue("CreatedBy", value); }

		}

		  
		[XmlAttribute("ModifiedOn")]
		public DateTime? ModifiedOn 
		{
			get { return GetColumnValue<DateTime?>("ModifiedOn"); }

			set { SetColumnValue("ModifiedOn", value); }

		}

		  
		[XmlAttribute("ModifiedBy")]
		public string ModifiedBy 
		{
			get { return GetColumnValue<string>("ModifiedBy"); }

			set { SetColumnValue("ModifiedBy", value); }

		}

		  
		[XmlAttribute("IsDeleted")]
		public bool IsDeleted 
		{
			get { return GetColumnValue<bool>("IsDeleted"); }

			set { SetColumnValue("IsDeleted", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public LMSData.UserGroupMapCollection UserGroupMapRecords()
		{
			return new LMSData.UserGroupMapCollection().Where(UserGroupMap.Columns.GroupID, GroupID).Load();
		}

		public LMSData.CourseGroupMapCollection CourseGroupMapRecords()
		{
			return new LMSData.CourseGroupMapCollection().Where(CourseGroupMap.Columns.GroupID, GroupID).Load();
		}

		#endregion
		
			
		
		//no foreign key tables defined (0)
		
		
		
		#region Many To Many Helpers
		
		 
		public LMSData.UserCollection GetUserCollection() { return Group.GetUserCollection(this.GroupID); }

		public static LMSData.UserCollection GetUserCollection(int varGroupID)
		{
			SubSonic.QueryCommand cmd = new SubSonic.QueryCommand(
				"SELECT * FROM User INNER JOIN UserGroupMap ON "+
				"User.UserID=UserGroupMap.UserID WHERE UserGroupMap.GroupID=@GroupID", Group.Schema.Provider.Name);
			
			cmd.AddParameter("@GroupID", varGroupID, DbType.Int32);
			IDataReader rdr = SubSonic.DataService.GetReader(cmd);
			UserCollection coll = new UserCollection();
			coll.LoadAndCloseReader(rdr);
			return coll;
		}

		
		public static void SaveUserMap(int varGroupID, UserCollection items)
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			QueryCommand cmdDel = new QueryCommand("DELETE FROM UserGroupMap WHERE GroupID=@GroupID", Group.Schema.Provider.Name);
			cmdDel.AddParameter("@GroupID", varGroupID);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (User item in items)
			{
				UserGroupMap varUserGroupMap = new UserGroupMap();
				varUserGroupMap.SetColumnValue("GroupID", varGroupID);
				varUserGroupMap.SetColumnValue("UserID", item.GetPrimaryKeyValue());
				varUserGroupMap.Save();
			}

		}

		public static void SaveUserMap(int varGroupID, System.Web.UI.WebControls.ListItemCollection itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM UserGroupMap WHERE GroupID=@GroupID", Group.Schema.Provider.Name);
			cmdDel.AddParameter("@GroupID", varGroupID);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (System.Web.UI.WebControls.ListItem l in itemList) 
			{
				if (l.Selected) 
				{
					UserGroupMap varUserGroupMap = new UserGroupMap();
					varUserGroupMap.SetColumnValue("GroupID", varGroupID);
					varUserGroupMap.SetColumnValue("UserID", l.Value);
					varUserGroupMap.Save();
				}

			}

		}

		public static void SaveUserMap(int varGroupID , int[] itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM UserGroupMap WHERE GroupID=@GroupID", Group.Schema.Provider.Name);
			cmdDel.AddParameter("@GroupID", varGroupID);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (int item in itemList) 
			{
				UserGroupMap varUserGroupMap = new UserGroupMap();
				varUserGroupMap.SetColumnValue("GroupID", varGroupID);
				varUserGroupMap.SetColumnValue("UserID", item);
				varUserGroupMap.Save();
			}

		}

		
		public static void DeleteUserMap(int varGroupID) 
		{
			QueryCommand cmdDel = new QueryCommand("DELETE FROM UserGroupMap WHERE GroupID=@GroupID", Group.Schema.Provider.Name);
			cmdDel.AddParameter("@GroupID", varGroupID);
			DataService.ExecuteQuery(cmdDel);
		}

		
		 
		public LMSData.CourseCollection GetCourseCollection() { return Group.GetCourseCollection(this.GroupID); }

		public static LMSData.CourseCollection GetCourseCollection(int varGroupID)
		{
			SubSonic.QueryCommand cmd = new SubSonic.QueryCommand(
				"SELECT * FROM Course INNER JOIN CourseGroupMap ON "+
				"Course.CourseID=CourseGroupMap.CourseID WHERE CourseGroupMap.GroupID=@GroupID", Group.Schema.Provider.Name);
			
			cmd.AddParameter("@GroupID", varGroupID, DbType.Int32);
			IDataReader rdr = SubSonic.DataService.GetReader(cmd);
			CourseCollection coll = new CourseCollection();
			coll.LoadAndCloseReader(rdr);
			return coll;
		}

		
		public static void SaveCourseMap(int varGroupID, CourseCollection items)
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			QueryCommand cmdDel = new QueryCommand("DELETE FROM CourseGroupMap WHERE GroupID=@GroupID", Group.Schema.Provider.Name);
			cmdDel.AddParameter("@GroupID", varGroupID);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (Course item in items)
			{
				CourseGroupMap varCourseGroupMap = new CourseGroupMap();
				varCourseGroupMap.SetColumnValue("GroupID", varGroupID);
				varCourseGroupMap.SetColumnValue("CourseID", item.GetPrimaryKeyValue());
				varCourseGroupMap.Save();
			}

		}

		public static void SaveCourseMap(int varGroupID, System.Web.UI.WebControls.ListItemCollection itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM CourseGroupMap WHERE GroupID=@GroupID", Group.Schema.Provider.Name);
			cmdDel.AddParameter("@GroupID", varGroupID);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (System.Web.UI.WebControls.ListItem l in itemList) 
			{
				if (l.Selected) 
				{
					CourseGroupMap varCourseGroupMap = new CourseGroupMap();
					varCourseGroupMap.SetColumnValue("GroupID", varGroupID);
					varCourseGroupMap.SetColumnValue("CourseID", l.Value);
					varCourseGroupMap.Save();
				}

			}

		}

		public static void SaveCourseMap(int varGroupID , int[] itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM CourseGroupMap WHERE GroupID=@GroupID", Group.Schema.Provider.Name);
			cmdDel.AddParameter("@GroupID", varGroupID);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (int item in itemList) 
			{
				CourseGroupMap varCourseGroupMap = new CourseGroupMap();
				varCourseGroupMap.SetColumnValue("GroupID", varGroupID);
				varCourseGroupMap.SetColumnValue("CourseID", item);
				varCourseGroupMap.Save();
			}

		}

		
		public static void DeleteCourseMap(int varGroupID) 
		{
			QueryCommand cmdDel = new QueryCommand("DELETE FROM CourseGroupMap WHERE GroupID=@GroupID", Group.Schema.Provider.Name);
			cmdDel.AddParameter("@GroupID", varGroupID);
			DataService.ExecuteQuery(cmdDel);
		}

		
		#endregion
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varGroupName,string varDescription,DateTime? varCreatedOn,string varCreatedBy,DateTime? varModifiedOn,string varModifiedBy,bool varIsDeleted)
		{
			Group item = new Group();
			
			item.GroupName = varGroupName;
			
			item.Description = varDescription;
			
			item.CreatedOn = varCreatedOn;
			
			item.CreatedBy = varCreatedBy;
			
			item.ModifiedOn = varModifiedOn;
			
			item.ModifiedBy = varModifiedBy;
			
			item.IsDeleted = varIsDeleted;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varGroupID,string varGroupName,string varDescription,DateTime? varCreatedOn,string varCreatedBy,DateTime? varModifiedOn,string varModifiedBy,bool varIsDeleted)
		{
			Group item = new Group();
			
				item.GroupID = varGroupID;
				
				item.GroupName = varGroupName;
				
				item.Description = varDescription;
				
				item.CreatedOn = varCreatedOn;
				
				item.CreatedBy = varCreatedBy;
				
				item.ModifiedOn = varModifiedOn;
				
				item.ModifiedBy = varModifiedBy;
				
				item.IsDeleted = varIsDeleted;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string GroupID = @"GroupID";
			 public static string GroupName = @"GroupName";
			 public static string Description = @"Description";
			 public static string CreatedOn = @"CreatedOn";
			 public static string CreatedBy = @"CreatedBy";
			 public static string ModifiedOn = @"ModifiedOn";
			 public static string ModifiedBy = @"ModifiedBy";
			 public static string IsDeleted = @"IsDeleted";
						
		}

		#endregion
	}

}

